package com.mlink.mapper;


import com.mlink.entity.RecordEntity;
import com.mlink.entity.RecordInfoEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RecordMapper {

	@Select("<script> " +
			"SELECT * " +
			"FROM record " +
			" <where> " +
			" <if test=\"userID != null\">user_id=#{userID}</if> " +
			" <if test=\"projectID != null\"> AND project_id=#{projectID}</if> " +
			" <if test=\"date != null\"> AND date=#{date}</if> " +
			" </where> " +
			" </script> ")
		//查询项目
//	@Select("SELECT * FROM record  where user_id =#{userID} and  project_id =#{projectID} and date =#{date}  limit #{start},#{num}")

//	@Select("SELECT * FROM record   limit #{start},#{num}")

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "optDate", column = "opt_date")

	})

	List<RecordEntity> getAll(@Param("start")Integer start, @Param("num")Integer num,  @Param("userID")Integer userID, @Param("projectID")Integer projectID,  @Param("date")String date);



	// 获取日志列表信息
	@Select("<script> " +
			"SELECT r.id, r.user_id, r.project_id,r.date,r.working_hour, u.nick_name, p.name as projectName " +
			"FROM record  as  r " +
			"LEFT JOIN users u " +
			"ON r.user_id=u.id  " +
			"LEFT JOIN project p  " +
			"ON r.project_id =p.id " +
			" <where> " +
			" <if test=\"userID != null\">r.user_id=#{userID}</if> " +
			" <if test=\"projectID != null\"> AND r.project_id=#{projectID}</if> " +
			" <if test=\"date != null\"> AND r.date=#{date}</if> " +
			" </where> " +
			" ORDER BY r.date DESC  limit #{start},#{num}" +
			" </script> ")
	//查询项目

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "nickName", column = "nick_name")

	})

	List<RecordInfoEntity> getRecordList(@Param("start")Integer start, @Param("num")Integer num, @Param("userID")Integer userID, @Param("projectID")Integer projectID, @Param("date")String date);





	@Select("SELECT * FROM record  WHERE id = #{id}")
	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "optDate", column = "opt_date")
	})
	RecordEntity getOne(int id);


	//增加一个
	@Insert("INSERT INTO record(user_id ,project_id ,record,date,working_hour,opt_date) VALUES(#{userID}, #{projectID}, #{record},#{date},#{workingHour},#{optDate})")
//	@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = int.class)
	int insert(RecordEntity record);



	//是否存在
	@Select("SELECT * FROM record  WHERE user_id = #{userID} and project_id = #{projectID} and date = #{date}")
	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour")
	})
	List<RecordEntity> isEixt(@Param("userID")int userID, @Param("projectID")int projectID, @Param("date")String date);




    @Update("UPDATE record SET user_id=#{userID},project_id=#{projectID},record=#{record}, date=#{date},working_hour=#{workingHour},opt_date =#{optDate} WHERE user_id = #{userID} and project_id = #{projectID} and date = #{date}" )
    int updateInfo(RecordEntity record);

	//
	// 更新
     @Update("UPDATE record SET user_id=#{userID},project_id=#{projectID},record=#{record},date=#{date},working_hour=#{workingHour},opt_date=#{optDate} WHERE id = #{id} ")
     int update(RecordEntity record);

//
    //删除
	@Delete("DELETE FROM record WHERE id =#{id}")
	int delete(int id);




}